﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using AgileToDo.Common;
using Data;
using Entity;

namespace AgileToDo
{
    /// <summary>
    /// UTodoItem.xaml 的交互逻辑
    /// </summary>
    public partial class UTodoItem : UserControl
    {
        public UTodoItem()
        {
            InitializeComponent();
        }

        private bool _isEdit=false;
        public bool IsEdit
        {

            get { return _isEdit; }
            set
            {
                _isEdit = value;
                if(value)
                {
                    this.tbx.Visibility = Visibility.Visible;
                    this.label.Visibility = Visibility.Collapsed;
                    //this.border.Height += 10;
                    this.tbx.Focus();
                }
                else
                {
                    this.tbx.Visibility = Visibility.Collapsed;
                    this.label.Visibility = Visibility.Visible;
                   // this.border.Height -= 10;
                    DbManager.ToDoContext.SaveChanges();
                }
            }
        }

        private void UserControl_MouseDoubleClick(object sender, MouseButtonEventArgs e)
        {
            if(this.cbxFinished.IsChecked.HasValue
                &&this.cbxFinished.IsChecked.Value)
            {
                return;
            }
            IsEdit = true;
        }


        private void tbx_LostFocus(object sender, RoutedEventArgs e)
        {
            IsEdit = false;
            Debug.WriteLine("lose fouce");
        }

        private void UserControl_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            e.Handled = true;
        }

        private void Border_MouseMove(object sender, MouseEventArgs e)
        {
          //  this.imgClose.Visibility=Visibility.Visible;;
        }

        private void border_MouseLeave(object sender, MouseEventArgs e)
        {
            //if (!IsMouseOver)
            //{
            //    this.imgClose.Visibility = Visibility.Collapsed;
            //}
         
        }

        private void Image_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {   
            var result = UMessageBox.Show("删除确认", "是否确定删除该待办？",MessageBoxButton.YesNo);
            if (result.HasValue && result.Value)
            {
                var st = this.Parent as StackPanel;
                st.Children.Remove(this);
                DbManager.ToDoContext.ToDos.Remove(this.DataContext as ToDo);
                DbManager.ToDoContext.SaveChanges();
            }
        }

        private void cbxFinished_Click(object sender, RoutedEventArgs e)
        {
            DbManager.ToDoContext.SaveChanges();
        }
    }
}
